package com.mjw.tender.service;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mjw.tender.api.TbUserService;
import com.mjw.tender.domain.TbUser;
import com.mjw.tender.dto.PageDto;
import com.mjw.tender.mapper.TbUserMapper;
import com.mjw.tender.service.fallback.TbUserServiceFallBack;
import org.apache.dubbo.config.annotation.Service;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class TbUserServiceImpl implements TbUserService {

    @Resource
    TbUserMapper tbUserMapper;

    @Override
//    @SentinelResource(value = "getByUsername", fallback = "getByUsernameFallback", fallbackClass = TbUserServiceFallBack.class)
    public TbUser get(String username) {
        Example example = new Example(TbUser.class);
        example.createCriteria().andEqualTo("username", username);
        return tbUserMapper.selectOneByExample(example);
    }

    @Override
    public int add(TbUser tbUser) {
        return tbUserMapper.insertSelective(tbUser);
    }

    @Override
    public Long addReturnId(TbUser tbUser) {
        tbUserMapper.addReturnId(tbUser);
        return tbUser.getId();
    }

    @Override
    public int update(TbUser tbUser) {
        return tbUserMapper.updateByPrimaryKeySelective(tbUser);
    }

    @Override
    public int deleteUsers(List<Long> ids) {
        Example example = new Example(TbUser.class);
        example.createCriteria().andIn("id",ids);
        return tbUserMapper.deleteByExample(example);
    }


    @Override
    public List<TbUser> search(TbUser tbUser) {
        return tbUserMapper.search(tbUser);
    }

    /**
     *
     * @param currPage 当前页
     * @param pageSize  每页显示数量
     * @return
     */
    @Override
    public PageDto<TbUser> getUsersByPage(Integer currPage, Integer pageSize) {
        Page page  = PageHelper.startPage(currPage,pageSize);
        List<TbUser> tbUserList = new ArrayList<TbUser>(tbUserMapper.selectAll());//TODO 可以去掉密码列
        long total = page.getTotal();
        PageDto<TbUser> pageDto = new PageDto<TbUser>(tbUserList,total);
        return pageDto;
    }
}
